# coding=utf-8
# 开庭公告实体解析

import json
import sys
sys.path.append("..")
sys.path.append("../../")

from i_entity_extractor.extractors.default.default_extractor import DefaultExtractor
from common_parser import CommonParser
from i_entity_extractor.common_parser_lib import toolsutil

class KtggExtractor(DefaultExtractor):
    def __init__(self, topic_info, log):
        DefaultExtractor.__init__(self, topic_info, log)
        self.seps = [',', ':', '\t']
        self.parser_obj = CommonParser(self.parser_tool, log)


    def entity_extract(self, parse_info, extract_data):
        '''开庭公告实体解析入口'''
        url = parse_info.base_info.url
        province = unicode(extract_data.get("province", ""))
        entity_data_list  = []
        extract_data_list = self.parser_obj.before_parser(extract_data)
        for extract_data in extract_data_list:
            content = extract_data.get("content","")
            if content:
                entity_data = self.parser_obj.get_data_from_content(extract_data)
            else:
                entity_data = self.parser_obj.get_data_from_litigants(extract_data)

            entity_data_list.append(entity_data)

        return entity_data_list


if __name__ == '__main__':

    import pytoml
    import sys

    sys.path.append('../../')
    from conf import get_config
    from bdp.i_crawler.i_extractor.ttypes import BaseInfo, ExtractInfo, PageParseInfo

    with open('../../entity.toml', 'rb') as config:
        config = pytoml.load(config)
    conf = get_config(config)
    import common

    topic_id = 34

    from entity_extractor_route import EntityExtractorRoute

    route = EntityExtractorRoute()
    topic_info = route.all_topics.get(topic_id, None)
    obj = KtggExtractor(topic_info, common.log)

    import time
    begin_time = time.time()
    extract_data = {
    "province" : "新疆",
    "site" : "117.190.16.231",
    "content" : "本院定于2016年12月6日8时40分在本院第八法庭公开开庭审理上诉人陕西坤和房地产开发有限责任公司与被上诉人薛建民占有物返还纷一案",

}


    src_url = "http://zxgk.sccourt.gov.cn/webapp/area/scsfgk/sfgk/ktgg/ajax.jsp?HZPOST=eyJjdXJyZW50UGFnZSI6IDQxMiwgIm1ocHAiOiAiMSIsICJuYXYiOiAiMDIiLCAib3B0IjogInFi%0Aa3QifQ%3D%3D%0A"
    data = json.dumps(extract_data)
    extract_info = ExtractInfo(ex_status=2, extract_data=data,topic_id=34)
    base_info = BaseInfo(url=src_url)
    parser_info = PageParseInfo(base_info=base_info, extract_info=extract_info)
    entity_data_list = obj.entity_extract(parser_info, extract_data)
    for entity_data in entity_data_list:
        print "-----------------------------"
        entity_data = obj.after_extract(src_url, entity_data, extract_data)
        print src_url
        for key, value in entity_data.items():
            if isinstance(value, list):
                for i in value:
                    print key, ":", i
            elif isinstance(value, dict):
                for key2, value2 in value.items():
                    print key2, ":", value2
            else:
                print key, ":", value

        print "time_cost:",time.time() - begin_time
